package com.jf.spring.transaction;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-10-29
 * Time:19:46
 */
@Repository
public class GoodDAO {
    @Resource(name = "template")
    private JdbcTemplate jdbcTemplate;
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void buyGood(int goodId) {
        String sql = "update goods set stock = stock - 1 where id = ?";
        jdbcTemplate.update(sql, goodId);
    }
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void buyGood(int goodId, int num) {
        String sql = "update goods set stock = stock - ? where id = ?";
        jdbcTemplate.update(sql, num, goodId);
    }
    @Transactional
    public void buyGood2(int goodId, int num) {
        String sql = "update goods set stock = stock - ? where id = ?";
        jdbcTemplate.update(sql, num, goodId);
    }
    @Transactional
    public Integer viewGoodStock(int goodId) {
        String sql = "select stock from goods where id = ?";
        return jdbcTemplate.queryForObject(sql, Integer.class, goodId);
    }
}
